package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class JiandanWrite {
 
 public static void main(String[] args) throws FileNotFoundException {
        readExcel();
 }
 /**
  * 如果以一个模版创建数据的话，就只有模版，没有数据 只能读取数据
  *
  */
 public static void readExcel() throws FileNotFoundException {
   //要生成的文件
   String path = "d:\\test.xls";
   //以这个模版去生成文件
   String src = "d:\\test2.xls";
   InputStream ins = new FileInputStream(src);
   OutputStream output = new FileOutputStream(path);
   HSSFWorkbook hworkBook = null;  
   HSSFSheet writableSheet =  null;
    try {
    
    POIFSFileSystem fs = new POIFSFileSystem(ins);
    //这个是只读Excel工作簿
    hworkBook = new HSSFWorkbook(fs);
    writableSheet = hworkBook.getSheetAt(1);
    // 数据也没添加进去 使用读取模版Excel文件的 对象不能写入数据
    HSSFRow row = writableSheet.createRow(5);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("我是第5行第一个！");
    System.out.println("第五行的内容："+cell.getStringCellValue());
   row.createCell(0).setCellValue("111");
    row.createCell(1).setCellValue("222");
    row.createCell(2).setCellValue("333");
    row.createCell(3).setCellValue("eeeeee");
    row.createCell(4).setCellValue("123455");
   
    //使用读取模版Excel文件的 对象能读取数据 OK
    /*HSSFRow readRow = writableSheet.getRow(0);
    HSSFCell readCell = readRow.getCell(0);
    String  value = readCell.getStringCellValue();
    System.out.print("读取到的第一个数据："+value);*/
    hworkBook.write(output);
    output.flush();
   }catch(Exception e){
    e.printStackTrace();
   }finally{
    try {
     if(output!=null)
     output.close();
     if(ins != null)
     ins.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
 }  
 //直接创建Excel文件 然后往里面写入数据 OK
 public static void createExcel(String[] args) throws FileNotFoundException {
  String path = "e:/单一资金信托计划要素表EXCEL模板(2003版)_test.xls";
  String src = "e:/test/单一资金信托计划要素表EXCEL模板(2003版).xls";
  InputStream ins = new FileInputStream(src);
  OutputStream output = new FileOutputStream(path);
   HSSFWorkbook hworkBook = null;  
   HSSFSheet writableSheet =  null;
   try {
    //Excel 可读可写工作簿
    hworkBook = new HSSFWorkbook();
   writableSheet = hworkBook.createSheet("表");
//   byte i = 4;
   //添加Sheet的数据
   HSSFRow row = writableSheet.createRow(1);
   HSSFCell cell = row.createCell(0);
   cell.setCellValue("我是第5行第一个！");
   //row.createCell(0).setCellValue("111");
   row.createCell(1).setCellValue("222");
   row.createCell(2).setCellValue("333");
   row.createCell(3).setCellValue("eeeeee");
   row.createCell(4).setCellValue("123455");
   System.out.println("第五行的内容："+cell.getStringCellValue());
   hworkBook.write(output);
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   try {
    output.close();
    ins.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}